﻿/// <reference path="vswd-ext_2.2.js" />

/*****************************************************
*****************************************************/

//var  hasstaticsensorfunction = false;
//if(typeof(staticsensor) !='undefined'){
//    if(staticsensor){
//        hasstaticsensorfunction = staticsensor;
//    }
//}

String.prototype.trim= function(){  
    // 用正则表达式将前后空格  
    // 用空字符串替代。  
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}
  
function createNameEditor() {
    return new Ext.grid.GridEditor(new Ext.form.TextField({ allowBlank: false, maxLength: 50 }));
}

function createUserNoteEditor() {
    return new Ext.grid.GridEditor(new Ext.form.TextArea({ allowBlank: true, maxLength: 1000,height:60 }));
}

function createCellphoneEditor() {
    var editor = new Ext.grid.GridEditor(new Ext.form.TextField({
            allowBlank: true,
            maxLength: 32,
            maxText: _Message_MaxText + ' 32',
            blankText: _NewForText_CanNotEmpty,
            regex: /^[0-9.]{5,32}$/,
            regexText: _NewForText_CellValid
        }));
    return editor;
}

function createTelephoneEditor() {
    var editor = new Ext.grid.GridEditor(new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        maxText: _Message_MaxText + ' 32',
        blankText: _NewForText_CanNotEmpty,
        regex: /^[0-9.]{5,32}$/,
        regexText: _NewForText_PhoneValid
    }));
    return editor;
}

function createEmailEditor() {
    return new Ext.grid.GridEditor(new Ext.form.TextField({
        maxLength: 255,
        maxText: _Message_MaxText + ' 255',
        vtype: 'email',
        vtypeText: _NewForText_EmailWrong
    }));
}

function createAddressEditor() {
    return new Ext.grid.GridEditor(new Ext.form.TextField({
        maxLength: 255,
        maxText: _Message_MaxText + ' 255',
        regex: /^[\s\S]{1,255}$/,
        regexText: _NewForText_AddressValid
    }));
}

function createDisabledEditor() {
    return new Ext.grid.GridEditor(new Ext.form.TextField({
        disabled: true,
        maxLength: 255,
        maxText: _Message_MaxText + ' 255'
    }));
}

function createPermissionComboEditor() {
    var cmb = new Ext.form.ComboBox({
        allowBlank: false,
        blankText: _NewForText_CanNotEmpty,
        labelStyle: 'padding-left:5px',
        fieldLabel: _NewForText_Permission,
        mode: "local",
        store: new Ext.data.SimpleStore({
            fields: ["id", "type"],
            data: [[1, _NewForText_NormalUser], [2, _NewForText_Administrator]]
        }),
        valueField: "type",
        displayField: "type",
        triggerAction: "all",
        editable: false,
        hiddenName: "mypermission"
    });
    var grideditor = new Ext.grid.GridEditor(cmb);
    return grideditor;
}

function createDatePicker() {
    var d = new Ext.grid.GridEditor(new Ext.form.DateField({
        maxText: _Message_MaxText + ' 255',
        labelStyle: 'padding-left:5px',
        fieldLabel: _CommonFields_ExpiredTime,
        allowBlank: true,
        blankText: _NewForText_CanNotEmpty,
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d'
    }));
    return d;
}

function createStatusAccount() {
    var d = new Ext.grid.GridEditor(new Ext.form.ComboBox({
        allowBlank: false,
        blankText: _NewForText_CanNotEmpty,
        labelStyle: 'padding-left:5px',
        fieldLabel: _ImageButtonText_StatusAccount,
        mode: "local",
        store: new Ext.data.SimpleStore({
            fields: ["id", "text"],
            data: [[0, _TrackerMainUserDevice_No], [1, _TrackerMainUserDevice_Yes],[-1, _TrackerMain_Lock]]
        }),
        valueField: "text",
        displayField: "text",
        triggerAction: "all",
        editable: false
    }));

    return d;
}

function createNumberEditor() {
    var d = new Ext.grid.GridEditor(new Ext.form.NumberField({
        maxLength: 255,
        maxText: _Message_MaxText + ' 255',
        regex: /^[\s\S]{1,255}$/,
        regexText: _NewForText_AddressValid
    }));
    return d;
}

//customer property grid header text
function setMyPropertyGridHeader(propGrid) {
    propGrid.colModel.nameText = _NewForText_OptionText;
    propGrid.colModel.valueText = _NewForText_ValueText;
    propGrid.colModel.config[0].width = 120;
    propGrid.colModel.config[0].header = _NewForText_OptionText
    propGrid.colModel.config[1].header = _NewForText_ValueText;
}

function msgUpdateFailure() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_Failure,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgUpdateFailurePasswodWrong() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_Failure + ',' + _NewForText_PasswordWrong,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgUpdateFailureDeviceIDWrong() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_Failure + ',' + _NewForText_DeviceIDWrong,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgUpdateSuccess() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_Success,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.INFO
    });
}

function msgRetypePasswordNotSame() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_PasswordNotSame,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgRetypeDeviceIDNotSame() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_DeviceIDNotSame,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgWarning(msgtext) {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: msgtext,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.WARNING
    });
}

function msgAccountPasswordWrong() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_AccountOrPasswordWrong,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.ERROR
    });
}

function msgChangePasswordSuccess() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_PasswordChanged,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.INFO
    });
}

function msgChangePasswordFaield() {
    Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
    Ext.Msg.show({
        title: _TrackerMain_Title,
        msg: _NewForText_PasswordChangeFailed,
        buttons: Ext.Msg.OK,
        animEl: 'elId',
        icon: Ext.MessageBox.ERROR
    });
}

function createPasswordWindow(form) {
    var pwdwin = new Ext.Window({
        height: 180,
        width: 400,
        closeAction: 'close',
        plain: true,
        modal: 'true',
        buttonAlign: "center",
        constrain:true,
        title: _NewForText_ChangePassword,
        items: [form]
    });

    return pwdwin;
}

function createPasswordForm(nodeid, nodetype, fnwinclose) {
    var pwdform = new Ext.form.FormPanel({
        bodyStyle: "padding-top:5px",
        width: 380,
        height: 140,
        defaultType: "textfield",
        items: [{
            name: "oldpassword",
            labelStyle: 'padding-left:5px',
            fieldLabel: _NewForText_OldPassword,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            inputType: 'password',
            width: 200
        }, {
            id: "password",
            name: "password",
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _NewForText_NewPassword,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese,
            inputType: 'password',
            width: 200
        }, {
            id: "retypepassword",
            name: "retypepassword",
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _NewForText_RetypePassword,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese,
            inputType: 'password',
            width: 200
            }],
            buttons: [
            { text: _NewForText_Confirm,
                handler: function() {
                    if (pwdform.getForm().isValid()) {
                        if (pwdform.get("retypepassword").getValue() == pwdform.get("password").getValue()) {
                            pwdform.form.submit({
                                url: "HandlerChangePassword?uid=" + nodeid + "&type=" + nodetype,
                                method: "POST",
                                success: function(form, action) {
                                    //after success
                                    var flag = action.result.success;
                                    if (flag == 'true') {
                                        msgChangePasswordSuccess();
                                        if (fnwinclose)
                                            fnwinclose();
                                    }
                                    else if (flag = "iserror") {
                                        msgChangePasswordFaield();
                                    }
                                },
                                failure: function(form, action) {
                                    msgUpdateFailurePasswodWrong();
                                }
                            })
                        }
                        else
                            msgRetypePasswordNotSame();
                    }
                }
            },
        { text: _NewForText_Reset,
            handler: function() {
                pwdform.getForm().reset()
            }
        },
        { text: _NewForText_Cancel,
            handler: function() {
                if (fnwinclose)
                    fnwinclose();
            }
        }
        ]
        });
        return pwdform;
    }

    
    function refreshDevice() {
        devicestore.load();
    }

    ShowMaster = function (node) {
        securityMaster = 2;

        var aedbtndisable = parseInt(permission) == 2 ? false : true;

        var txtUserPassword = new Ext.form.TextField({
            name: "userpassword",
            inputType: 'password',
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _ImageButtonText_UserPassWord,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            width: 200,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese
        });

        var txtRetypeUserPassword = new Ext.form.TextField({
            name: "retypeuserpassword",
            inputType: 'password',
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _ImageButtonText_RetypePassword,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            width: 200,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese
        });
		
		// Call ajax to check username is existed
        var groupAccountExist = function () {
			var userIsExist = false;
            var userAccountName = addnewgroupform.getForm().findField('useraccount').getValue();
            Ext.Ajax.request({
                url: 'HandlerIsExist',
                method: "POST",
                params: { type: 'groupAccount', item: userAccountName },
                success: function (response, options) {
                    var responseArray = Ext.util.JSON.decode(response.responseText);
                    if (responseArray.success == true) {
                        userIsExist = true;
                    } else {
                        userIsExist = false;
                    }
                }
            });
            return !userIsExist;
        }
	//-----------------------------------------------------------
	//k-phu: add new group form
        var addnewgroupform = new Ext.form.FormPanel({
            bodyStyle: "padding-top:5px",
            width: 380,
            title: _ImageButtonText_GroupTitleForm,
            height: 500,
            defaultType: "textfield",
            items: [
			    {
			        name: "useraccount",
			        maxLength: 50,
			        maxText: _Message_MaxText + ' 50',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserAccount,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        validator: groupAccountExist,
			        invalidText: _ImageButtonText_UserAccountExist,
			        regex: /^[\w\d]{6,30}$/,
			        regexText: _NewForText_CanNotChinese,
			        width: 200
			    },
			    txtUserPassword,
			    txtRetypeUserPassword,
			    {
			        name: "fullname",
			        maxLength: 50,
			        maxText: _Message_MaxText + ' 50',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserName,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200
			    }, {
			        name: "usercellphone",
			        maxLength: 32,
			        maxText: _Message_MaxText + ' 32',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserCellPhone,
			        allowBlank: true,
			        width: 200,
			        regex: /^[0-9.]{5,32}$/,
			        regexText: _NewForText_CellValid
			    }, {
			        name: "usertelephone",
			        maxLength: 32,
			        maxText: _Message_MaxText + ' 32',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserTelePhone,
			        allowBlank: true,
			        width: 200,
			        regex: /^[0-9.]{5,32}$/,
			        regexText: _NewForText_PhoneValid

			    }, {
			        name: "useremail",
			        maxLength: 255,
			        maxText: _Message_MaxText + ' 255',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserEmail,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200,
			        vtype: 'email',
			        vtypeText: _NewForText_EmailWrong
			    }, {
			        name: "useraddress",
			        maxLength: 255,
			        maxText: _Message_MaxText + ' 255',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserAddress,
			        allowBlank: true,
			        width: 200,
			        regex: /^[\s\S]{1,255}$/,
			        regexText: _NewForText_AddressValid
			    }           
                , {
                    xtype: 'numberfield',
                    name: 'usertrackerlimit',
                    allowBlank: false,
                    blankText: _NewForText_CanNotEmpty,
                    labelStyle: 'padding-left:5px',
                    fieldLabel: _CommonFields_TrackerLimit
                }, {
                    xtype: 'datefield',
                    name: 'userexpiredtime',
                    allowBlank: false,
                    blankText: _NewForText_CanNotEmpty,
                    format: "Y-m-d",
                    labelStyle: 'padding-left:5px',
                    fieldLabel: _CommonFields_ExpiredTime
                }
                , {
                    xtype: 'combo',
                    name: "accountstatus",
                    allowBlank: false,
                    blankText: _NewForText_CanNotEmpty,
                    labelStyle: 'padding-left:5px',
                    fieldLabel: _TrackerMain_SendEmailWhenAlarm,
                    mode: "local",
                    store: new Ext.data.SimpleStore({
                        fields: ["id", "text"],
                        data: [[0, _TrackerMainUserDevice_No], [1, _TrackerMainUserDevice_Yes], [-1, _TrackerMain_Lock]]
                    }),
                    valueField: "id",
                    displayField: "text",
                    triggerAction: "all",
                    editable: false,
                    hiddenName: "accountstatus"
                }, {
                    xtype: 'textarea',
                    name: "usernote",
                    maxLength: 4000,
                    maxText: _Message_MaxText + ' 4000',
                    labelStyle: 'padding-left:5px',
                    fieldLabel: _ImageButtonText_UserNote,
                    allowBlank: true,
                    width: 200,
                    height: 60
                }],
            buttons: [
            { text: _NewForText_Confirm,
                handler: function () {
                    if (addnewgroupform.getForm().isValid()) {
						//k-phu: Check password and retype password
                        if (txtRetypeUserPassword.getValue() == txtUserPassword.getValue()) {
                            
							//k-phu: Show waiting box
							Ext.MessageBox.show({
                                msg: _NewForText_Saving,
                                progressText: _NewForText_Saving + '...',
                                width: 300,
                                wait: true,
                                waitConfig: { interval: 200 },
                                icon: 'download'
                            });
							// Submit form
                            addnewgroupform.form.submit({
                                url: "HandlerAddOrRemoveUser?groupid=" + node.id + "&type=AddNew&mode=groupAccount&mypermission=1111",
                                method: "POST",
                                success: function (form, action) {
                                    //after success
                                    var flag = action.result.success;
                                    if (flag == "true") {
										//k-phu: Reload tree
                                        refreshDevice();
                                        addnewgroupwindow.hide();
                                        root.reload();
                                        treenode.expandAll();
                                        msgUpdateSuccess();
                                    }
                                    else if (flag == "iserror") {
                                        msgWarning(_NewForText_AddNewUserFailedByExist);
                                    }
                                    else if (flag == "nopermission") {
                                        msgWarning(_NewForText_NoPermissionAddUser);
                                    }
                                    else if (flag == "fail") {
                                        msgWarning('Error: ' + _NewForText_Failure);
                                    }
                                    else if (flag == "maxlimit") {
                                        msgWarning('Error: ' + _NewForText_AdminMaxLimit);
                                    }
                                    else {
                                        msgWarning('Error: ' + _NewForText_Failure);
                                    }

                                },
                                failure: function (form, action) {
                                    msgUpdateFailure();
                                }
                            })
                        }
                        else {
                            msgRetypePasswordNotSame();
                        }
                    }
                }
            },
            { text: _NewForText_Reset,
                handler: function () {
                    addnewgroupform.getForm().reset();
                }
            },
            { text: _NewForText_Cancel,
                handler: function () {
                    addnewgroupwindow.hide()
                }
            }
        ]
        });

        var addnewgroupwindow = new Ext.Window({
            height: 530,
            width: 400,
            closeAction: 'hide',
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            constrain: true,
            items: [addnewgroupform]
        });

        var btnAddGroup = new Ext.Button({
            text: _ImageButtonText_GroupAdd,
            disabled: aedbtndisable,
            handler: function () {
                addnewgroupform.getForm().reset()
                addnewgroupwindow.show();
            }
        });

        var btnChangePassword = new Ext.Button({
            disabled: aedbtndisable,
            text: _NewForText_ChangePassword,
            handler: function () {
                var pwdfrm = createPasswordForm(node.id, "M", function () {
                    pwdwin.close();
                });
                var pwdwin = createPasswordWindow(pwdfrm);
                pwdwin.show();
            }
        });

        var formButton = new Ext.Panel({
            width: 400,
            height: 10,
            border:false,
            buttons: [btnAddGroup, btnChangePassword]
        });

        var form = new Ext.form.FormPanel({
            width: 450,
            height: 450,
            padding: '10',
            items: [formButton]
        });

        GridMain(node, form, "Home");
    }
	//-----------------------------------------------------------
    //this function is use for show the group infomation and take this info to the treepanel
    ShowGroup = function (node) {

        //alert(node.id);

        var aedbtndisable = parseInt(permission) == 2 ? false : true;
        var otherbtndisable = parseInt(permission) > 0 ? false : true;

        var btnChangePassword = new Ext.Button({
            text: _NewForText_ChangePassword,
            disabled: otherbtndisable,
            handler: function () {
                var pwdfrm = createPasswordForm(node.id, "G", function () {
                    pwdwin.close();
                });
                var pwdwin = createPasswordWindow(pwdfrm);
                pwdwin.show();
            }
        });


        var btnConfirm = new Ext.Button({
            text: _NewForText_Confirm,
            disabled: otherbtndisable,
            handler: function () {
                if (propertyGrid.getSource()["Name"] != '') {
                    propertyGrid.stopEditing();
                    Ext.MessageBox.show({
                        msg: _NewForText_Saving,
                        progressText: _NewForText_Saving,
                        width: 300,
                        wait: true,
                        waitConfig: { interval: 200 }
                    });
                    Ext.Ajax.request({
                        url: 'HandlerUpdateGroupInfo',
                        success: function (result) {
                            Ext.MessageBox.hide();

                            var flag = eval(result.responseText);

                            if (flag == true) {
                                refreshDevice();
                                root.reload();
                                treenode.expandAll();
                                msgUpdateSuccess();
                            }
                            else {
                                msgUpdateFailure();
                            }
                        },
                        failure: function () {
                            Ext.MessageBox.hide();
                            msgUpdateFailure();
                        },
                        params: {
                            Name: propertyGrid.getSource().Name,
                            Email: propertyGrid.getSource().Email,
                            Email2: propertyGrid.getSource().Email2,
                            Email3: propertyGrid.getSource().Email3,
                            AccountStatus: getAccountStatusFromText(propertyGrid.getSource().AccountStatus),
                            Address: propertyGrid.getSource().Address,
                            CellPhone: propertyGrid.getSource().CellPhone,
                            TelePhone: propertyGrid.getSource().TelePhone,
                            Usernote: propertyGrid.getSource().UserNote,
                            usertrackerlimit: propertyGrid.getSource().UserLimit,
                            userexpiredtime: propertyGrid.getSource().ExpiredTime,
                            GroupID: node.id
                        }
                    })
                }
            }
        });

        var propertyGrid;

        //Check perssion to edit what you want -> again permission

        var propertyGrid;

        if (securityMaster == 2) {
            propertyGrid = new Ext.ux.grid.PropertyGrid({
                header: false,
                autoHeight: true,
                autoWidth: true,
                width: 400,
                fields: [{ id: 'Name', dataIndex: 'Name', header: _CommonFields_GroupName, editable: true, editor: createNameEditor() },
                 { id: 'CellPhone', dataIndex: 'CellPhone', header: _CommonFields_Cellphone, editable: true, editor: createCellphoneEditor() },
                 { id: 'TelePhone', dataIndex: 'TelePhone', header: _CommonFields_Telephone, editable: true, editor: createTelephoneEditor() },
                 { id: 'Email', dataIndex: 'Email', header: _CommonFields_Email, editable: true, editor: createEmailEditor() },
                //{ id: 'Email2', dataIndex: 'Email2', header: _CommonFields_Email + ' 2', editable: true, editor: createEmailEditor() },
                //  { id: 'Email3', dataIndex: 'Email3', header: _CommonFields_Email + ' 3', editable: true, editor: createEmailEditor() },
                 {id: 'AccountStatus', dataIndex: 'AccountStatusExpression', header: _ImageButtonText_StatusAccount, editable: true, editor: createStatusAccount() },
                 { id: 'Address', dataIndex: 'Address', header: _CommonFields_Address, editable: true, editor: createAddressEditor() },
                 { id: 'RegisterTime', dataIndex: 'RegisterTime', header: _CommonFields_RegisterTime, editable: true, editor: createDisabledEditor() },
                 { id: 'ExpiredTime', dataIndex: 'ExpiredTime', header: _CommonFields_ExpiredTime, editable: true, editor: createDatePicker() },
                 { id: 'UserLimit', dataIndex: 'UserLimit', header: _CommonFields_TrackerLimit, editable: true, editor: createNumberEditor() },
                  { id: 'UserNote', dataIndex: 'UserNote', header: _ImageButtonText_UserNote, editable: true, editor: createUserNoteEditor() }
                 ]

            });
        }
        else if (securityMaster == 1) {
            propertyGrid = new Ext.ux.grid.PropertyGrid({
                header: false,
                autoHeight: true,
                autoWidth: true,
                width: 400,
                fields: [{ id: 'Name', dataIndex: 'Name', header: _CommonFields_GroupName, editable: true, editor: createNameEditor() },
                 { id: 'CellPhone', dataIndex: 'CellPhone', header: _CommonFields_Cellphone, editable: true, editor: createCellphoneEditor() },
                 { id: 'TelePhone', dataIndex: 'TelePhone', header: _CommonFields_Telephone, editable: true, editor: createTelephoneEditor() },
                 { id: 'Email', dataIndex: 'Email', header: _CommonFields_Email, editable: true, editor: createEmailEditor() },
                //{ id: 'Email2', dataIndex: 'Email2', header: _CommonFields_Email + ' 2', editable: true, editor: createEmailEditor() },
                //  { id: 'Email3', dataIndex: 'Email3', header: _CommonFields_Email + ' 3', editable: true, editor: createEmailEditor() },
                 {id: 'AccountStatus', dataIndex: 'AccountStatusExpression', header: _ImageButtonText_StatusAccount, editable: true, editor: createDisabledEditor() },
                 { id: 'Address', dataIndex: 'Address', header: _CommonFields_Address, editable: true, editor: createAddressEditor() },
                 { id: 'RegisterTime', dataIndex: 'RegisterTime', header: _CommonFields_RegisterTime, editable: true, editor: createDisabledEditor() },
                 { id: 'ExpiredTime', dataIndex: 'ExpiredTime', header: _CommonFields_ExpiredTime, editable: true, editor: createDisabledEditor() },
                 { id: 'UserLimit', dataIndex: 'UserLimit', header: _CommonFields_TrackerLimit, editable: true, editor: createDisabledEditor() }
                 ]

            });
        }

        if (permission >= 0) {
            propertyGrid.addButton(btnConfirm);
            propertyGrid.addButton(btnChangePassword);
        }

        propertyGrid.addListener('beforerender', function (propGrid) {
            setMyPropertyGridHeader(propGrid);
        });

        var propertyStore = new Ext.data.JsonStore({
            autoLoad: true,
            url: 'HandlerGetGroupInfo?GroupID=' + node.id,
            root: 'data',
            fields: ['Name', 'Email', 'Address', 'CellPhone', 'TelePhone', 'RegisterTime', 'ExpiredTime', 'AccountStatus', 'UserLimit', 'AccountStatusExpression', 'UserNote'],
            listeners: {
                load: {
                    fn: function (store, records, options) {
                        var propGrid = propertyGrid
                        if (propGrid) {
                            var r = store.getAt(0);
                            //                            var b = r.get('EmailWhenAlarm');
                            //                            r.set('EmailWhenAlarm', getBooleanString(b));
                            propGrid.setSource(store.getAt(0).data);
                        }
                    }
                }
            }
			
        });

        // Check userAccount is exist
        var userAccountExist = function () {
            var userAccountName = addnewuserform.getForm().findField('useraccount').getValue();
			var userIsExist = false;
            Ext.Ajax.request({
                url: 'HandlerIsExist',
                method: "POST",
                params: { type: 'userAccount', item: userAccountName },
                success: function (response, options) {
                    var responseArray = Ext.util.JSON.decode(response.responseText);
                    if (responseArray.success == true) {
                        userIsExist= true;
                    } else {
                        userIsExist= false;
                    }
                }
            });
            return !userIsExist;
        };
		
        var txtUserPassword = new Ext.form.TextField({
            name: "userpassword",
            inputType: 'password',
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _ImageButtonText_UserPassWord,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            width: 200,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese
        });

        var txtRetypeUserPassword = new Ext.form.TextField({
            name: "retypeuserpassword",
            inputType: 'password',
            maxLength: 50,
            maxText: _Message_MaxText + ' 50',
            labelStyle: 'padding-left:5px',
            fieldLabel: _ImageButtonText_RetypePassword,
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            width: 200,
            regex: /^[\w\d]*$/,
            regexText: _NewForText_CanNotChinese
        });
        //add new user
        var addnewuserform = new Ext.form.FormPanel({
            bodyStyle: "padding-top:5px",
            width: 380,
            title: _NewForText_AddNewUser,
            height: 500,
            defaultType: "textfield",
            items: [
			    {
			        name: "useraccount",
			        maxLength: 50,
			        maxText: _Message_MaxText + ' 50',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserAccount,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        validator: userAccountExist,
			        invalidText: _ImageButtonText_UserAccountExist,
			        regex: /^[\w\d]{6,30}$/,
			        regexText: _NewForText_CanNotChinese,
			        width: 200
			    },
			    txtUserPassword,
			    txtRetypeUserPassword,
			    {
			        name: "fullname",
			        maxLength: 50,
			        maxText: _Message_MaxText + ' 50',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserName,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200
			    }, {
			        name: "usercellphone",
			        maxLength: 32,
			        maxText: _Message_MaxText + ' 32',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserCellPhone,
			        allowBlank: true,
			        width: 200,
			        regex: /^[0-9.]{5,32}$/,
			        regexText: _NewForText_CellValid
			    }, {
			        name: "usertelephone",
			        maxLength: 32,
			        maxText: _Message_MaxText + ' 32',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserTelePhone,
			        allowBlank: true,
			        width: 200,
			        regex: /^[0-9.]{5,32}$/,
			        regexText: _NewForText_PhoneValid

			    }, {
			        name: "useremail",
			        maxLength: 255,
			        maxText: _Message_MaxText + ' 255',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserEmail,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200,
			        vtype: 'email',
			        vtypeText: _NewForText_EmailWrong
			    }
                ,
			    {
			        name: "useraddress",
			        maxLength: 255,
			        maxText: _Message_MaxText + ' 255',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserAddress,
			        allowBlank: true,
			        width: 200,
			        regex: /^[\s\S]{1,255}$/,
			        regexText: _NewForText_AddressValid
			    }, {
			        xtype: 'numberfield',
			        name: 'usertrackerlimit',
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _CommonFields_TrackerLimit
			    }, {
			        xtype: 'datefield',
			        name: 'userexpiredtime',
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        format: "Y-m-d",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _CommonFields_ExpiredTime
			    }, {
			        xtype: 'combo',
			        name: "mypermission",
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _NewForText_Permission,
			        mode: "local",
			        store: new Ext.data.SimpleStore({
			            fields: ["id", "type"],
			            data: [[1, _NewForText_NormalUser], [2, _NewForText_Administrator]]
			        }),
			        valueField: "id",
			        displayField: "type",
			        triggerAction: "all",
			        editable: false,
			        hiddenName: "mypermission"

			    }, {
			        xtype: 'combo',
			        name: "accountstatus",
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _TrackerMain_SendEmailWhenAlarm,
			        mode: "local",
			        store: new Ext.data.SimpleStore({
			            fields: ["id", "text"],
			            data: [[0, _TrackerMainUserDevice_No], [1, _TrackerMainUserDevice_Yes], [-1, _TrackerMain_Lock]]
			        }),
			        valueField: "id",
			        displayField: "text",
			        triggerAction: "all",
			        editable: false,
			        hiddenName: "accountstatus"
			    }, {
			        xtype: 'textarea',
			        name: "usernote",
			        maxLength: 4000,
			        maxText: _Message_MaxText + ' 4000',
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserNote,
			        allowBlank: true,
			        width: 200,
			        height: 60
			    }],
            buttons: [
            { text: _NewForText_Confirm,
                handler: function () {
                    if (addnewuserform.getForm().isValid()) {
                        if (txtRetypeUserPassword.getValue() == txtUserPassword.getValue()) {
                            //pwd same,update
                            Ext.MessageBox.show({
                                msg: _NewForText_Saving,
                                progressText: _NewForText_Saving + '...',
                                width: 300,
                                wait: true,
                                waitConfig: { interval: 200 },
                                icon: 'download'
                            });
                            addnewuserform.form.submit({
                                url: "HandlerAddOrRemoveUser?groupid=" + node.id + "&type=AddNew&mode=userAccount",
                                method: "POST",
                                success: function (form, action) {
                                    //after success
                                    var flag = action.result.success;
                                    if (flag == "true") {
                                        refreshDevice();
                                        addnewuerwindow.hide();
                                        root.reload();
                                        treenode.expandAll();
                                        msgUpdateSuccess();

                                    }
                                    else if (flag == "iserror") {
                                        msgWarning(_NewForText_AddNewUserFailedByExist);
                                    }
                                    else if (flag == "nopermission") {
                                        msgWarning(_NewForText_NoPermissionAddUser);
                                    }
                                    else if (flag == "maxlimit") {
                                        msgWarning('Error: ' + _NewForText_AdminMaxLimit);
                                    }
                                    else {
                                        msgWarning('Error: ' + _NewForText_Failure);
                                    }
                                },
                                failure: function (form, action) {
                                    msgUpdateFailure();
                                }
                            })
                        }
                        else {
                            msgRetypePasswordNotSame();
                        }
                    }
                }
            },
            { text: _NewForText_Reset,
                handler: function () {
                    addnewuserform.getForm().reset();
                }
            },
            { text: _NewForText_Cancel,
                handler: function () {
                    addnewuerwindow.hide()
                }
            }
        ]
        });

        var addnewuerwindow = new Ext.Window({
            height: 530,
            width: 400,
            closeAction: 'hide',
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            constrain: true,
            items: [addnewuserform]
        });

        //add old user
        var addolduserform = new Ext.form.FormPanel({
            bodyStyle: "padding-top:5px",
            width: 380,
            title: _NewForText_AddOldUser,
            height: 180,
            defaultType: "textfield",
            items: [
			    {
			        name: "useraccount",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserAccount,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200
			    }, {
			        name: "userpassword",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_UserPassWord,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        width: 200
			    }],
            buttons: [
            {
                text: _NewForText_Submit,
                handler: function () {
                    if (addolduserform.getForm().isValid()) {
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        addolduserform.form.submit({
                            url: "HandlerAddOrRemoveUser?groupid=" + node.id + "&type=AddOld&mode=userAccount",
                            method: "POST",
                            success: function (form, action) {
                                //after success
                                var flag = action.result.success;
								
                                if (flag == 'true' || flag == true) {
                                    refreshDevice();
                                    msgUpdateSuccess();
                                    addolduerwindow.hide();
                                    root.reload();
                                    treenode.expandAll();
                                }
                                else if (flag == "iserror") {
                                    msgWarning(_NewForText_IsExistAccount);
                                }
                                else if (flag == "maxlimit") {
                                    msgWarning('Error: ' + _NewForText_AdminMaxLimit);
                                }
                                else {
                                    msgWarning('Error: ' + _NewForText_Failure);
                                }
                            },
                            failure: function (form, action) {
                                msgAccountPasswordWrong();
                            }
                        })
                    }
                }
            },
            { text: _ImageButtonText_Reset,
                handler: function () {
                    addolduserform.getForm().reset();
                }
            },
            { text: _ImageButtonText_Cancel,
                handler: function () {
                    addolduerwindow.hide();
                }
            }
        ]
        });

        var addolduerwindow = new Ext.Window({
            height: 200,
            closeAction: 'hide',
            width: 400,
            plain: true,
            modal: 'true',
            closable: false,
            buttonAlign: "center",
            constrain: true,
            items: [addolduserform]
        })
        var groupsuserstore = new Ext.data.ArrayStore({
            autoDestroy: true,
            fields: ['Account', 'Name'],
            url: 'HandlerGetAGroupAllUserAccount?groupId='+ node.id,
            autoLoad: true
        });
        //delete user
        var radOnlyDeleteRelation = new Ext.form.Radio({
            boxLabel: _Message_OnlyDeleteRelation,
            value: 1,
            autoWidth: true,
            checked: true,
            name: 'raddeltype'
        });
        var radDeleteRelationAndData = new Ext.form.Radio({
            boxLabel: _Message_DeleteRelationAndData,
            value: 2,
            autoWidth: true,
            name: 'raddeltype'
        });
        //        var radRemoveUser = new Ext.form.RadioGroup({
        //            items:[radOnlyDeleteRelation,radDeleteRelationAndData]
        //        });

        var removeolduserform = new Ext.form.FormPanel({
            bodyStyle: "padding-top:5px",
            width: 580,
            title: _NewForText_RemoveUser,
            height: 250,
            defaultType: "textfield",
            items: [{
                xtype: 'multiselect',
                fieldLabel: _NewForText_RemoveUser,
                name: 'multiselect',
                width: 400,
                height: 120,
                valueField: 'Account',
                displayField: 'Name',
                store: groupsuserstore,
                allowBlank: false,
                ddReorder: true
            }, radOnlyDeleteRelation, radDeleteRelationAndData],
            buttons: [
            { text: _NewForText_Remove,
                handler: function () {
                    var valueArray = String(removeolduserform.getForm().findField("multiselect").getValue());
                    if (valueArray != "") {
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        removeolduserform.form.submit({
                            url: "HandlerAddOrRemoveUser/Remove",
                            method: "POST",
                            success: function (form, action) {
                                //after success
                                var flag = action.result.success;
                                if (flag == 'true') {
                                    refreshDevice();
                                    removeolduerwindow.hide();
                                    root.reload();
                                    treenode.expandAll();
                                    msgUpdateSuccess();
                                }
                                else if (flag = "iserror") {
                                    msgWarning(_NewForText_UserNotExistInGroup);
                                }
                            },
                            timeout: 60000,
                            failure: function (form, action) {
                                msgUpdateFailurePasswodWrong();
                            },
                            params: { groupid: node.id,
                                useraccounts: valueArray,
                                deltype: radOnlyDeleteRelation.checked ? 0 : 1,
                                type: "Remove"
                            }
                        });
                    }
                    else
                        msgWarning(_CustomerConfigResource_NoSelected);
                }
            },


        { text: _NewForText_Cancel,
            handler: function () {
                removeolduerwindow.hide()
            }
        }
        ]
        });
        var removeolduerwindow = new Ext.Window({
            height: 300,
            closeAction: 'hide',
            width: 600,
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            closable: false,
            constrain: true,
            items: [removeolduserform]
        });
        var form;

        if (permission >= 0) {

            //Administrator permission
            form = new Ext.form.FormPanel({
                width: 450,
                height: 450,
                items: [propertyGrid],
                buttons: [
                { text: _NewForText_AddNewUser,
                    disabled: aedbtndisable,
                    handler: function () {
                        addnewuserform.getForm().reset()
                        addnewuerwindow.show();
                    }
                },
                { text: _NewForText_AddOldUser,
                    disabled: aedbtndisable, handler: function () {
                        addolduserform.getForm().reset();
                        addolduerwindow.show()
                    }
                },
                { text: _NewForText_RemoveUser,
                    disabled: aedbtndisable, handler: function () {
                        removeolduserform.getForm().reset();
                        removeolduerwindow.show();
                        groupsuserstore.reload();
                    }
                }
            ]
            });
        }
        else {
            form = new Ext.form.FormPanel({
                width: 450,
                height: 450,
                items: [propertyGrid]
            });
        }
        GridMain(node, form, "Group");

    }

    //this function is use for show the user infomation and take this info to the treepanel
    ShowUser = function (node) {
        var aedbtndisable = parseInt(permission) == 2 ? false : true;
        var otherbtndisable = parseInt(permission) > 0 ? false : true;
        var imgname = "man";
        var imagebutton = new Ext.ux.ImageButton({
            imgPath: 'Content/images/man.png',
            disabled: otherbtndisable,
            imgWidth: 32,
            imgHeight: 32,
            labelStyle: 'padding-left:5px;padding-top:15px',
            fieldLabel: _ImageButtonText_TrackerIcon,
            handler: function () {
                iconPanel.setVisible(true);
            }
        });
        var iconStore = new Ext.data.JsonStore({
            url: 'HandlerGetAllTrackerIconInfo',
            root: 'images',
            fields: ['name'],
            autoLoad: true
        });

        var iconTpl = new Ext.XTemplate(
		'<tpl for=".">',
            '<div class="thumb-wrap" id="{name}">',
		    '<div class="thumb"><img src="Content/images/trackericon/{name}" title="{name}"></div>',
		    '</div>',
        '</tpl>',
        '<div class="x-clear"></div>'
	);
        var iconPanel = new Ext.Panel({
            frame: true,
            cls: 'images-view',
            autoHeight: true,
            autoHeight: true,
            collapsible: true,
            layout: 'fit',
            header: false,
            title: _TrackerMain_ChooseICON,
            hidden: true,
            items: new Ext.DataView({
                store: iconStore,
                tpl: iconTpl,
                autoHeight: true,
                multiSelect: true,
                overClass: 'x-view-over',
                itemSelector: 'div.thumb-wrap',
                plugins: [
                new Ext.DataView.DragSelector(),
                new Ext.DataView.LabelEditor({ dataIndex: 'name' })
            ],
                listeners: {
                    'click': function (dr, index) {
                        var records = dr.getSelectedRecords();
                        var name = String(records[0].data.name);
                        //imagebutton.setIcon('images/trackericon/' + name);
                        //imagebutton.setText(name.substring(0, name.indexOf('.')));
                        imagebutton.enable('Content/images/trackericon/' + name);
                        imgname = name.substring(0, name.indexOf('.'));
                        iconPanel.setVisible(false);
                    }
                }
            })
        });

        var btnChangePassword = new Ext.Button({
            disabled: otherbtndisable,
            text: _NewForText_ChangePassword,
            handler: function () {
                var pwdfrm = createPasswordForm(node.id, "U", function () {
                    pwdwin.close();
                });
                var pwdwin = createPasswordWindow(pwdfrm);
                pwdwin.show();
            }
        });

        var btnConfirm;

        var propertyGrid
        if (grouplogin == "1" && permission >= 0) {
            btnConfirm = new Ext.Button({
                text: _NewForText_Confirm,
                disabled: otherbtndisable,
                handler: function () {
                    if (propertyGrid.getSource()["Name"] != '') {
                        propertyGrid.stopEditing();
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        Ext.Ajax.request({
                            url: 'HandlerUpdateUserInfo',
                            success: function (result) {
                                Ext.MessageBox.hide();
                                var flag = eval(result.responseText);
                                if (flag == true) {
                                    refreshDevice();
                                    root.reload();
                                    treenode.expandAll();
                                    msgUpdateSuccess();
                                }
                                else {
                                    msgUpdateFailure();
                                }
                            },
                            failure: function () {
                                Ext.MessageBox.hide();
                                msgUpdateFailure();
                            },
                            params: {
                                Name: propertyGrid.getSource().Name,
                                Email: propertyGrid.getSource().Email,
                                Email2: propertyGrid.getSource().Email2,
                                Email3: propertyGrid.getSource().Email3,
                                AccountStatus:  getAccountStatusFromText(propertyGrid.getSource().AccountStatus),
                                Address: propertyGrid.getSource().Address,
                                CellPhone: propertyGrid.getSource().CellPhone,
                                TelePhone: propertyGrid.getSource().TelePhone,
                                Permission: getPermissionIDFromTexxt(propertyGrid.getSource().Permisstion),
                                ExpiredTime: propertyGrid.getSource().ExpiredTime,
                                UserLimit: propertyGrid.getSource().UserLimit,
                                UserID: node.id,
                                UserNote: propertyGrid.getSource().UserNote
                            }
                        })
                    }
                    else {
                        msgWarning(_Message_InfoIncomplete);
                    }
                }
            });

            propertyGrid = new Ext.ux.grid.PropertyGrid({
                header: false,
                autoHeight: true,
                width: 400,
                fields: [{ id: 'Name', dataIndex: 'Name', header: _CommonFields_UserName, editable: true, editor: createNameEditor() },
                     { id: 'CellPhone', dataIndex: 'CellPhone', header: _CommonFields_Cellphone, editable: true, editor: createCellphoneEditor() },
                     { id: 'TelePhone', dataIndex: 'TelePhone', header: _CommonFields_Telephone, editable: true, editor: createTelephoneEditor() },
                     { id: 'Email', dataIndex: 'Email', header: _CommonFields_Email, editable: true, editor: createEmailEditor() },
                //{ id: 'Email2', dataIndex: 'Email2', header: _CommonFields_Email + ' 2', editable: true, editor: createEmailEditor() },
                //{ id: 'Email3', dataIndex: 'Email3', header: _CommonFields_Email + ' 3', editable: true, editor: createEmailEditor() },
                     {id: 'AccountStatus', dataIndex: 'AccountStatusExpression', header: _TrackerMain_SendEmailWhenAlarm, editable: true, editor: createStatusAccount() },
                     { id: 'Address', dataIndex: 'Address', header: _CommonFields_Address, editable: true, editor: createAddressEditor() },
                     { id: 'Permisstion', dataIndex: 'PermissionExpression', header: _NewForText_Permission, editable: true, editor: createPermissionComboEditor() },
                     { id: 'UserLimit', dataIndex: 'UserLimit', header: _CommonFields_TrackerLimit, editable: true, editor: createNumberEditor() },
                     { id: 'RegisterTime', dataIndex: 'RegisterTime', header: _CommonFields_RegisterTime, editable: true, editor: createDisabledEditor() },
                     { id: 'ExpiredTime', dataIndex: 'ExpiredTime', header: _CommonFields_ExpiredTime, editable: true, editor: createDatePicker() },
                     { id: 'UserNote', dataIndex: 'UserNote', header: _ImageButtonText_UserNote, editable: true, editor: createUserNoteEditor() }
                     ]
            });

        }
        else {
            btnConfirm = new Ext.Button({
                text: _NewForText_Confirm,
                disabled: otherbtndisable,
                handler: function () {
                    if (propertyGrid.getSource()["Name"] != '') {
                        propertyGrid.stopEditing();
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        Ext.Ajax.request({
                            url: 'HandlerUpdateUserInfo',
                            success: function (result) {
                                Ext.MessageBox.hide();
                                var flag = eval(result.responseText);
                                if (flag == true) {
                                    refreshDevice();
                                    root.reload();
                                    treenode.expandAll();
                                    msgUpdateSuccess();
                                }
                                else {
                                    msgUpdateFailure();
                                }
                            },
                            failure: function () {
                                Ext.MessageBox.hide();
                                msgUpdateFailure();
                            },
                            params: {
                                Name: propertyGrid.getSource().Name,
                                Email: propertyGrid.getSource().Email,
                                Email2: propertyGrid.getSource().Email2,
                                Email3: propertyGrid.getSource().Email3,
                                AccountStatus: getAccountStatusFromText(propertyGrid.getSource().AccountStatus),
                                Address: propertyGrid.getSource().Address,
                                CellPhone: propertyGrid.getSource().CellPhone,
                                TelePhone: propertyGrid.getSource().TelePhone,
                                Permission: getPermissionIDFromTexxt(propertyGrid.getSource().Permisstion),
                                ExpiredTime: propertyGrid.getSource().ExpiredTime,
                                UserLimit: propertyGrid.getSource().UserLimit,
                                UserID: node.id,
                                UserNote: propertyGrid.getSource().UserNote
                            }
                        })
                    }
                    else {
                        msgWarning(_Message_InfoIncomplete);
                    }
                }
            });
            propertyGrid = new Ext.ux.grid.PropertyGrid({
                header: false,
                autoHeight: true,
                width: 400,
                fields: [{ id: 'Name', dataIndex: 'Name', header: _CommonFields_UserName, editable: true, editor: createNameEditor() },
                     { id: 'CellPhone', dataIndex: 'CellPhone', header: _CommonFields_Cellphone, editable: true, editor: createCellphoneEditor() },
                     { id: 'TelePhone', dataIndex: 'TelePhone', header: _CommonFields_Telephone, editable: true, editor: createTelephoneEditor() },
                     { id: 'Email', dataIndex: 'Email', header: _CommonFields_Email, editable: true, editor: createEmailEditor() },
                // { id: 'Email2', dataIndex: 'Email2', header: _CommonFields_Email + ' 2', editable: true, editor: createEmailEditor() },
                // { id: 'Email3', dataIndex: 'Email3', header: _CommonFields_Email + ' 3', editable: true, editor: createEmailEditor() },
                     {id: 'AccountStatus', dataIndex: 'AccountStatusExpression', header: _TrackerMain_SendEmailWhenAlarm, editable: true, editor: createDisabledEditor() },
                     { id: 'Address', dataIndex: 'Address', header: _CommonFields_Address, editable: true, editor: createAddressEditor() },
                     { id: 'UserLimit', dataIndex: 'UserLimit', header: _CommonFields_TrackerLimit, editable: true, editor: createDisabledEditor() },
                     { id: 'RegisterTime', dataIndex: 'RegisterTime', header: _CommonFields_RegisterTime, editable: true, editor: createDisabledEditor() },
                     { id: 'ExpiredTime', dataIndex: 'ExpiredTime', header: _CommonFields_ExpiredTime, editable: true, editor: createDisabledEditor() }
                     ]
            });

        }

        if (permission >= 0) {
            propertyGrid.addButton(btnConfirm);
            propertyGrid.addButton(btnChangePassword);
        }

        propertyGrid.addListener('beforerender', function (propGrid) {
            setMyPropertyGridHeader(propGrid);
        });

        var propertyStore = new Ext.data.JsonStore({
            url: 'HandlerGetUserInfo',
            root: 'data',
            fields: ['Name', 'Email', 'Address', 'CellPhone', 'TelePhone', 'Permisstion', 'PermissionExpression', 'RegisterTime', 'ExpiredTime', 'UserLimit', 'Email2', 'AccountStatus', 'AccountStatusExpression', 'UserNote'],
            listeners: {
                load: {
                    fn: function (store, records, options) {
                        var propGrid = propertyGrid;
                        if (propGrid) {
                            var r = store.getAt(0);
//                            var b = r.get('EmailWhenAlarm');
//                            r.set('EmailWhenAlarm', getBooleanString(b));
                            propGrid.setSource(store.getAt(0).data);
                        }
                    }
                }
            }
			
        });

        propertyStore.load({ params: { UserID: node.id} })


        //judge snIMeiId whether exist
        var isok = true;
        snIMeiIdExist = function () {
            var snIMeiId = addnewdeviceform.getForm().findField('snimeiid').getValue();
			var isExist = false;
            Ext.Ajax.request({
                url: 'HandlerIsExist',
                method: "POST",
                params: { type: 'snIMeiId', item: snIMeiId },
                success: function (response, options) {
                    var responseArray = Ext.util.JSON.decode(response.responseText);
                    if (responseArray.success == true) {
                        isExist = true;
                    } else {
						isExist = false;
                    }
                },
                failure: function () {
                    //SetValue(true);
                }
            });
            return !isExist;
        }

        //        var fuelsensorInAdd = new Ext.form.ComboBox({
        //            labelStyle: 'padding-left:5px',
        //			fieldLabel: _TrackerMain_FuelSensor,
        //            editable: false,
        //            name: "fuelsensorid",
        //            allowBlank: true,
        //            mode: "local",
        //            store: new Ext.data.SimpleStore({
        //                    fields: [{name:"id",type:'integer'}, "name"],
        //                    data: [[0,_TrackerMain_Null],[1,_TrackerMain_FuelC],[2,_TrackerMain_FuelR],[3,_TrackerMain_FuelSensorV1]]
        //                    }),
        //            valueField: "id",
        //            displayField: "name",
        //            triggerAction: "all",
        //            hiddenName: "fuelsensorid",
        //            width: 200
        //        });
        //        fuelsensorInAdd.setValue(0);
        //        
        //        var tanksizeTextEditorInAdd = new Ext.form.NumberField({
        //            name:'tanksizevalue',
        //            labelStyle: 'padding-left:5px',
        //			fieldLabel: _TrackerMain_TankSize,
        //            maxLength:4,
        //            width: 200
        //        });

        var addnewdeviceform = new Ext.form.FormPanel({
            bodyStyle: "padding-top:5px",
            width: 380,
            title: _NewForText_AddNewDevice,
            height: 550,
            defaultType: "textfield",
            items: [
			{
			    name: "snimeiid",
			    maxLength: 32,
			    maxText: _Message_MaxText + ' 32',
			    labelStyle: 'padding-left:5px',
			    fieldLabel: _ImageButtonText_SnIMeiId,
			    allowBlank: false,
			    blankText: _NewForText_CanNotEmpty,
			    validator: snIMeiIdExist,
			    invalidText: _ImageButtonText_DeviceExist,
			    regex: /^[\w\d]*$/,
			    regexText: _NewForText_CanNotChinese,
			    width: 200
			}, {
			    name: "trackerpassword",
			    maxLength: 6,
			    maxText: _Message_MaxText + ' 6',
			    labelStyle: 'padding-left:5px',
			    fieldLabel: _ImageButtonText_TrackerPassword,
			    allowBlank: false,
			    blankText: _NewForText_CanNotEmpty,
			    regex: /^[0-9]{4,6}$/,
			    regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + ' 4-6',
			    width: 200
			}, {
			    name: "trackername",
			    maxLength: 50,
			    maxText: _Message_MaxText + ' 50',
			    labelStyle: 'padding-left:5px',
			    fieldLabel: _ImageButtonText_TrackerName,
			    allowBlank: false,
			    blankText: _NewForText_CanNotEmpty,
			    width: 200

			}, {
			    name: "trackersimnumber",
			    maxLength: 16,
			    maxText: _Message_MaxText + ' 16',
			    labelStyle: 'padding-left:5px',
			    fieldLabel: _ImageButtonText_TrackerSimNumber,
			    allowBlank: false,
			    blankText: _NewForText_CanNotEmpty,
			    regex: /^[0-9]{5,16}$/,
			    regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 5-16",
			    width: 200
			}, /*{
		        name:"apn",
		        maxLength:50,
		        maxText: _Message_MaxText+' 50',
		        labelStyle : 'padding-left:5px',
				fieldLabel:_ImageButtonText_Apn,
				regex:/^[\w\d]*$/,
                regexText:_NewForText_CanNotChinese,
                width:200
					
			},
			{
		        name:"gprsusername",
		        maxLength:50,
		        maxText: _Message_MaxText+' 50',
		        labelStyle : 'padding-left:5px',
				fieldLabel:_ImageButtonText_GprsUserName,
				regex:/^[\w\d]*$/,
                regexText:_NewForText_CanNotChinese,
                width:200
			},
			{
		        name:"gprspassword",
		        maxLength:50,
		        maxText: _Message_MaxText+' 50',
		        labelStyle : 'padding-left:5px',
				fieldLabel:_ImageButtonText_GprsPassWord,
				regex:/^[\w\d]*$/,
                regexText:_NewForText_CanNotChinese,
                width:200
					
			},*/{
			name: "trackertypeid",
			allowBlank: false,
			editable: false,
			labelStyle: 'padding-left:5px',
			xtype: "combo",
			fieldLabel: _ImageButtonText_TrackerType,
			mode: "local",
			//			store: new Ext.data.SimpleStore({
			//			    fields: ["id", "type"],
			//			    data: [[0, "GT30"], [1, "GT30X"], [2, "GT60"], [3, "VT300"], [4, "VT310"], [5, "MT80"], [6, "VT400"], [7, "MT88"], [8, "MVT600"], [9, "MVT100"], [10, "MVT340"], [11, "MVT380"]]
			//			}),
			store: new Ext.data.JsonStore({
			    url: 'HandlerGetAllModel',
			    fields: ["ModelName", "ModelIndex"],
			    autoLoad: true
			}),
			valueField: "ModelIndex",
			displayField: "ModelName",
			triggerAction: "all",
			hiddenName: "trackertypeid"
}, imagebutton, iconPanel],
            buttons: [
            { text: _NewForText_Submit,
                handler: function () {
                    if (addnewdeviceform.getForm().isValid()) {
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        addnewdeviceform.form.submit({
                            url: "HandlerAddOrRemoveDevice?userid=" + node.id + "&type=AddNew",
                            method: "POST",
                            params: { TrackerIconName: imgname },
                            success: function (form, action) {
                                //after success
                                var flag = action.result.success;
                                if (flag == 'true') {
                                    refreshDevice();
                                    msgUpdateSuccess();
                                    addnewdevicewindow.hide();
                                    root.reload();
                                    treenode.expandAll();
                                }
                                else if (flag == "iserror") {
                                    msgWarning(_NewForText_AddNewDeviceFailedByExist);
                                }
                                else if (flag == "nopermission") {
                                    msgWarning(_NewForText_NoPermissionAddDevice);
                                }
                                else if (flag == "maxlimit") {
                                    msgWarning(_NewForText_AdminMaxLimit);
                                }
                                else if (flag == "exist") {
                                    msgWarning(_NewForText_AddNewUserFailedByExistDevice);
                                }
                                else {
                                    msgWarning(_NewForText_AddNewDeviceFailedByExist);
                                }
                            },
                            failure: function (form, action) {
                                msgUpdateFailure();
                            }
                        })
                    }
                }
            },
        { text: _NewForText_Reset,
            handler: function () {
                addnewdeviceform.getForm().reset()
            }
        },
        { text: _NewForText_Cancel,
            handler: function () {
                addnewdevicewindow.hide()
            }
        }
        ]
        });

        //        if(hasstaticsensorfunction){
        //            //fuelsensorInAdd.setVisible(false);
        //            //tanksizeTextEditorInAdd.setVisible(false);
        //            //addnewdeviceform.insert(5,tanksizeTextEditorInAdd);
        //            addnewdeviceform.insert(5,fuelsensorInAdd);
        //            
        //        }

        var addnewdevicewindow = new Ext.Window({
            height: 600,
            closeAction: 'hide',
            width: 400,
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            constrain: true,
            items: [addnewdeviceform]
        });

        //add old device
        var addolddeviceform = new Ext.form.FormPanel({
            bodyStyle: 'padding-top:5px',
            width: 380,
            title: _NewForText_AddOldDevice,
            height: 200,
            defaultType: "textfield",
            items: [
			    {
			        name: "snimeiid",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_SnIMeiId,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        regex: /^[\w\d]*$/,
			        regexText: _NewForText_CanNotChinese,
			        width: 200
			    }, {
			        name: "trackersimnumber",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_TrackerSimNumber,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        regex: /^[0-9]{5,16}$/,
			        regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 5-16",
			        width: 200
			    }, {
			        name: "trackerpassword",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_TrackerPassword,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        regex: /^[0-9]{4,6}$/,
			        regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 4-6",
			        width: 200
			    }],
            buttons: [
                    { text: _NewForText_Submit,
                        handler: function () {
                            if (addolddeviceform.getForm().isValid()) {
                                Ext.MessageBox.show({
                                    msg: _NewForText_Saving,
                                    progressText: _NewForText_Saving + '...',
                                    width: 300,
                                    wait: true,
                                    waitConfig: { interval: 200 },
                                    icon: 'download'
                                });
                                addolddeviceform.form.submit({
                                    url: "HandlerAddOrRemoveDevice?userid=" + node.id + "&type=AddOld",
                                    method: "POST",
                                    success: function (form, action) {
                                        //after success
                                        var flag = action.result.success;
                                        if (flag == true || flag == 'true') {
                                            refreshDevice();
                                            addolddevicewindow.hide();
                                            root.reload();
                                            treenode.expandAll();
                                            msgUpdateSuccess();
                                        }
                                        else if (flag = "iserror") {
                                            msgWarning(_NewForText_IsExistDevice);
                                        }
                                    },
                                    failure: function (form, action) {
                                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                                        Ext.Msg.show({
                                            title: _TrackerMain_Title,
                                            msg: _NewForText_Failure + ',' + _NewForText_PasswordWrong + _NewForText_Or + _NewForText_SIMNumberWrong,
                                            buttons: Ext.Msg.OK,
                                            animEl: 'elId',
                                            icon: Ext.MessageBox.WARNING
                                        });
                                    }
                                })
                            }
                        }
                    },
                    { text: _NewForText_Reset,
                        handler: function () {
                            addolddeviceform.getForm().reset()
                        }
                    },
                    { text: _NewForText_Cancel,
                        handler: function () {
                            addolddevicewindow.hide()
                        }
                    }
            ]
        })


        var addolddevicewindow = new Ext.Window({
            height: 250,
            closeAction: 'hide',
            width: 400,
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            constrain: true,
            items: [addolddeviceform]
        });

        //remove device
        var removeolddeviceform = new Ext.form.FormPanel({
            bodyStyle: 'padding-top:5px',
            width: 380,
            title: _NewForText_RemoveDevice,
            height: 150,
            defaultType: "textfield",
            items: [
			    {
			        name: "snimeiid",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_SnIMeiId,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        regex: /^[\w\d]*$/,
			        regexText: _NewForText_CanNotChinese,
			        width: 200
			    }, {
			        name: "trackerpassword",
			        labelStyle: 'padding-left:5px',
			        fieldLabel: _ImageButtonText_TrackerPassword,
			        allowBlank: false,
			        blankText: _NewForText_CanNotEmpty,
			        regex: /^[0-9]{4,6}$/,
			        regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 4-6",
			        width: 200
			    }],
            buttons: [
            { text: _NewForText_Remove,
                handler: function () {
                    if (removeolddeviceform.getForm().isValid()) {
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        removeolddeviceform.form.submit({
                            url: "HandlerAddOrRemoveDevice?userid=" + node.id + "&type=Remove",
                            method: "POST",
                            success: function (form, action) {
                                //after success
                                var flag = action.result.success;
                                if (flag == true || flag =='true') {
                                    refreshDevice();
                                    removeolddevicewindow.hide();
                                    root.reload();
                                    treenode.expandAll();
                                    msgUpdateSuccess();

                                }
                                else if (flag = "iserror") {
                                    msgWarning(_NewForText_DeviceNotExistInUser);
                                }
                            },
                            timeout: 60000,
                            failure: function (form, action) {
                                msgUpdateFailurePasswodWrong();
                            }
                        })
                    }
                }
            },
            { text: _NewForText_Reset,
                handler: function () {
                    removeolddeviceform.getForm().reset()
                }
            },
            { text: _NewForText_Cancel,
                handler: function () {
                    removeolddevicewindow.hide()
                }
            }
        ]
        });

        var removeolddevicewindow = new Ext.Window({
            height: 200,
            width: 400,
            closeAction: 'hide',
            plain: true,
            modal: 'true',
            buttonAlign: "center",
            constrain: true,
            items: [removeolddeviceform]
        });

        var form;
        if (permission >= 0) {
            //administrator permission
            form = new Ext.form.FormPanel({
                width: 450,
                height: 450,
                items: [propertyGrid],
                buttons: [{ text: _NewForText_AddNewDevice,
                    disabled: aedbtndisable, handler: function () {

                        addnewdeviceform.getForm().reset();
                        addnewdevicewindow.show();
                    }
                },
                { text: _NewForText_AddOldDevice, disabled: aedbtndisable, handler: function () {
                    addolddeviceform.getForm().reset();
                    addolddevicewindow.show();
                }
                },
                { text: _NewForText_RemoveDevice, disabled: aedbtndisable, handler: function () {
                    removeolddeviceform.getForm().reset()
                    removeolddevicewindow.show();
                }
                }
            ]
            });
        }
        else {
            form = new Ext.form.FormPanel({
                width: 450,
                height: 450,
                items: [propertyGrid]
            });
        }
        GridMain(node, form, "User");
    }

    //this function is use for show the device infomation and take this info to the treepanel
    ShowDevice = function (node) {
        var aedbtndisable = parseInt(permission) == 2 ? false : true;
        var otherbtndisable = parseInt(permission) > 0 ? false : true;
        var parentNodeID = node.parentNode.id;
        var pwdtextfiled = new Ext.form.TextField({
            inputType: 'password',
            allowBlank: false
        });
        var devicepwdeditor = new Ext.grid.GridEditor(pwdtextfiled);

        var trackertypestore = new Ext.data.JsonStore({
            url: 'HandlerGetAllModel',
            fields: ["ModelName", "ModelIndex"],
            autoLoad: true
        });
        var trackertypeComboBox = new Ext.form.ComboBox({
            editable: false,
            name: "devicetypeid",
            allowBlank: false,
            mode: "local",
            //        store: new Ext.data.SimpleStore({
            //            fields: ["type", "trackertypeid"],
            //            data: [["GT30", 0], ["GT30X", 1], ["GT60", 2], ["VT300", 3], ["VT310", 4], ["MT80", 5], ["VT400", 6], ["MT88", 7], ["MVT600", 8], ["MVT100", 9], ["MVT340", 10], ["MVT380", 11]]
            //        }),
            store: trackertypestore,
            valueField: "ModelName",
            displayField: "ModelName",
            triggerAction: "all",
            hiddenName: "devicetypeid"
        });

        var iconStore = new Ext.data.JsonStore({
            url: 'HandlerGetAllTrackerIconInfo',
            root: 'images',
            fields: ['name'],
            autoLoad: true
        });

        var synBtnConfirm = new Ext.Button({
            text: _NewForText_UpdateAllTrackerIcon,
            disabled: otherbtndisable,
            style: 'margin-right:16px',
            handler: function () {
                if (propertyGrid.getSource().TrackerId != '' && propertyGrid.getSource().TrackerPassWord != '' && propertyGrid.getSource().TrackerName != '' && propertyGrid.getSource().TrackerSimNumber != '') {
                    propertyGrid.stopEditing();
                    Ext.MessageBox.show({
                        msg: _NewForText_Saving + '...',
                        progressText: _NewForText_Saving + '...',
                        width: 300,
                        wait: true,
                        waitConfig: { interval: 200 },
                        icon: 'download'
                    });
                    Ext.Ajax.request({
                        url: 'HandlerUpdateAllDeviceIcon',
                        success: function (result) {
                            Ext.MessageBox.hide();
                            //                        var flag = eval(result.responseText);
                            //                        if (flag == true) {
                            refreshDevice();
                            root.reload();
                            treenode.expandAll();
                            msgUpdateSuccess();
                            reloadAllLastlocations();
                            //                        }
                            //                        else {
                            //                            msgUpdateFailure();
                            //                        }
                        },
                        failure: function () {
                            Ext.MessageBox.hide();
                            msgUpdateFailure();
                        },
                        params: {
                            TrackerIconName: trackerIconText.getValue(),
                            ID: parentNodeID
                        }
                    })
                }
                else {
                    msgWarning(_Message_InfoIncomplete);
                }
            }
        });

        var iconTpl = new Ext.XTemplate(
		'<tpl for=".">',
            '<div class="thumb-wrap" id="{name}">',
		    '<div class="thumb"><img src="Content/images/trackericon/{name}" title="{name}"></div>',
		    '</div>',
        '</tpl>',
        '<div class="x-clear"></div>'
	);
        var iconPanel = new Ext.Panel({
            frame: true,
            width: 400,
            autoHeight: true,
            cls: 'images-view',
            collapsible: true,
            layout: 'form',
            title: _TrackerMain_ChooseICON,
            hidden: false,
            items: new Ext.DataView({
                store: iconStore,
                tpl: iconTpl,
                autoHeight: true,
                multiSelect: true,
                overClass: 'x-view-over',
                itemSelector: 'div.thumb-wrap',
                plugins: [
                new Ext.DataView.DragSelector(),
                new Ext.DataView.LabelEditor({ dataIndex: 'name' })
            ],
                listeners: {
                    'click': function (dr, index) {
                        var records = dr.getSelectedRecords();
                        var name = String(records[0].data.name);

                        propertyGrid.startEditing(5, 1);
                        trackerIconText.setRawValue(name.substring(0, name.indexOf('.')));
                    }
                }
            })
            //,buttons: [synBtnConfirm]
        });

        iconPanel.on('collapse', function () { iconPanel.setVisible(false); });
        iconPanel.on('hide', function () { iconPanel.expand(false); });
        var trackertypeGridEditor = new Ext.grid.GridEditor(trackertypeComboBox);
        var trackerIconText = new Ext.form.TextField({
            name: "deviceIconID",
            readOnly: true,
            allowBlank: true,
            //hidden : true,
            listeners: {
                scope: this,
                'focus': function () {
                    iconPanel.setVisible(true);
                }
            }
        });
        var trackerIconGridEditor = new Ext.grid.GridEditor(trackerIconText);
        var buttonChangeDeviceID = new Ext.Button({
            text: _NewForText_ChangeDeviceID,
            disabled: otherbtndisable,
            handler: function () {
                var isok = true;
                snIMeiIdExist = function () {
                    var snIMeiId = frmChangeDeviceID.getForm().findField('newsnimeiid').getValue();
                    Ext.Ajax.request({
                        url: 'HandlerIsExist',
                        method: "POST",
                        params: { type: 'snIMeiId', item: snIMeiId },
                        success: function (response, options) {
                            var responseArray = Ext.util.JSON.decode(response.responseText);
                            if (responseArray.success == true) {
                                SetValue(false);
                            } else {
                                SetValue(true);
                            }
                        },
                        failure: function () {
                            SetValue(true);
                        }
                    });
                    function SetValue(b) {
                        isok = b;
                    }
                    return isok;
                }
                var frmChangeDeviceID = new Ext.form.FormPanel({
                    bodyStyle: 'padding-top:5px',
                    width: 380,
                    title: _NewForText_ChangeDeviceID,
                    height: 200,
                    defaultType: "textfield",
                    items: [
			            {
			                name: "oldsnimeiid",
			                labelStyle: 'padding-left:5px',
			                fieldLabel: _NewForText_OldDeviceID,
			                allowBlank: false,
			                blankText: _NewForText_CanNotEmpty,
			                regex: /^[\w\d]*$/,
			                regexText: _NewForText_CanNotChinese,
			                width: 200
			            },
                    ////			            {
                    ////			                name: "trackersimnumber",
                    ////			                maxLength: 16,
                    ////			                maxText: _Message_MaxText + ' 16',
                    ////			                labelStyle: 'padding-left:5px',
                    ////			                fieldLabel: _ImageButtonText_TrackerSimNumber,
                    ////			                allowBlank: false,
                    ////			                blankText: _NewForText_CanNotEmpty,
                    ////			                regex: /^[0-9]{5,16}$/,
                    ////			                regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 5-16",
                    ////			                width: 200
                    ////			            },
                    ////			            {
                    ////			                name: "trackerpassword",
                    ////			                labelStyle: 'padding-left:5px',
                    ////			                fieldLabel: _ImageButtonText_TrackerPassword,
                    ////			                allowBlank: false,
                    ////			                blankText: _NewForText_CanNotEmpty,
                    ////			                regex: /^[0-9]{4,6}$/,
                    ////			                regexText: _NewForText_MustBeNumeric + _NewForText_LengthMustBetween + " 4-6",
                    ////			                width: 200
                    ////			            },
			            {
			            name: "newsnimeiid",
			            labelStyle: 'padding-left:5px',
			            fieldLabel: _NewForText_NewDeviceID,
			            allowBlank: false,
			            blankText: _NewForText_CanNotEmpty,
			            regex: /^[\w\d]*$/,
			            regexText: _NewForText_CanNotChinese,
			            validator: snIMeiIdExist,
			            invalidText: _ImageButtonText_DeviceExist,
			            width: 200
			        },
                        {
                            name: "renewsnimeiid",
                            labelStyle: 'padding-left:5px',
                            fieldLabel: _NewForText_ReNewDeviceID,
                            allowBlank: false,
                            blankText: _NewForText_CanNotEmpty,
                            regex: /^[\w\d]*$/,
                            regexText: _NewForText_CanNotChinese,
                            validator: snIMeiIdExist,
                            invalidText: _ImageButtonText_DeviceExist,
                            width: 200
                        }
			            ],
                    buttons: [
                    { text: _NewForText_Confirm,
                        handler: function () {
                            //renewsnimeiid
                            //msgRetypeDeviceIDNotSame
                            if (frmChangeDeviceID.getForm().isValid()) {
                                if (frmChangeDeviceID.form.items.items[1].getValue() == frmChangeDeviceID.form.items.items[2].getValue()) {
                                    frmChangeDeviceID.form.submit({
                                        url: "HandlerChangeSNIMEIID?DeviceID=" + node.id,
                                        method: "POST",
                                        success: function (form, action) {
                                            //after success
                                            var flag = action.result.success;
                                            if (flag == 'true') {
                                                refreshDevice();
                                                winChangeDeviceID.hide();
                                                root.reload();
                                                treenode.expandAll();
                                                msgUpdateSuccess();
                                            }
                                            else if (flag = "iserror") {
                                                msgWarning(_NewForText_Failure);
                                            }
                                        },
                                        failure: function (form, action) {
                                            msgUpdateFailureDeviceIDWrong();
                                        }
                                    })
                                }
                                else
                                    msgRetypeDeviceIDNotSame();
                            }
                        }
                    },
                    { text: _NewForText_Reset,
                        handler: function () {
                            frmChangeDeviceID.getForm().reset()
                        }
                    },
                    { text: _NewForText_Cancel,
                        handler: function () {
                            winChangeDeviceID.hide()
                        }
                    }
                ]

                });
                var winChangeDeviceID = new Ext.Window({
                    height: 250,
                    closeAction: 'close',
                    width: 400,
                    plain: true,
                    modal: 'true',
                    buttonAlign: "center",
                    constrain: true,
                    items: [frmChangeDeviceID]
                });

                winChangeDeviceID.show();
            }

        });

        var btnConfirm = new Ext.Button({
            text: _NewForText_Confirm,
            disabled: otherbtndisable,
            handler: function () {
                if (propertyGrid.getSource().TrackerId != '' && propertyGrid.getSource().TrackerPassWord != '' && propertyGrid.getSource().TrackerName != '' && propertyGrid.getSource().TrackerSimNumber != '') {
                    propertyGrid.stopEditing();
                    Ext.MessageBox.show({
                        msg: _NewForText_Saving + '...',
                        progressText: _NewForText_Saving + '...',
                        width: 300,
                        wait: true,
                        waitConfig: { interval: 200 },
                        icon: 'download'
                    });
                    Ext.Ajax.request({
                        url: 'HandlerUpdateDeviceInfo',
                        success: function (result) {
                            Ext.MessageBox.hide();
                          
                            var flag = eval(result.responseText);
                            if (flag == true) {
                                refreshDevice();
                                root.reload();
                                treenode.expandAll();
                                msgUpdateSuccess();
                                reloadSingleLastlocations(propertyGrid.getSource().TrackerName);
                            }
                            else {
                                msgUpdateFailure();
                            }
                        },
                        failure: function () {
                            Ext.MessageBox.hide();
                            msgUpdateFailure();
                        },
                        params: { TrackerID: propertyGrid.getSource().TrackerId,
                            TrackerPassword: propertyGrid.getSource().TrackerPassWord,
                            TrackerName: propertyGrid.getSource().TrackerName,
                            TrackerSimNumber: propertyGrid.getSource().TrackerSimNumber,
                            Apn: propertyGrid.getSource().APN,
                            GprsUserName: propertyGrid.getSource().GprsUserName,
                            GprsPassword: propertyGrid.getSource().GprsPassWord,
                            TrackerType: propertyGrid.getSource().TrackerType,
                            TrackerIconName: trackerIconText.getValue(),
                            //SensorId1: fuelsensorCombo.getValue(), //propertyGrid.getSource().SensorId1,
                            //TankSize:propertyGrid.getSource().TankSize,
                            //BaseMileage: propertyGrid.getSource().BaseMileage,
                            ID: node.id,
                            type :'Modify'
                        }
                    })
                }
                else {
                    msgWarning(_Message_InfoIncomplete);
                }
            }
        });

        var trackerFields = [{ id: 'SN_IMEI_ID', dataIndex: 'SN_IMEI_ID', header: _ImageButtonText_SnIMeiId, editable: true, editor: new Ext.grid.GridEditor(new Ext.form.TextField({ readOnly: true })) },
                  { id: 'TrackerPassWord', dataIndex: 'TrackerPassWord', header: _ImageButtonText_TrackerPassword, editable: true, editor: devicepwdeditor },
                  { id: 'TrackerName', dataIndex: 'TrackerName', header: _ImageButtonText_TrackerName, editable: true, editor: new Ext.grid.GridEditor(new Ext.form.TextField({ allowBlank: false })) },
                  { id: 'TrackerSimNumber', dataIndex: 'TrackerSimNumber', header: _ImageButtonText_TrackerSimNumber, editable: true, editor: new Ext.grid.GridEditor(new Ext.form.TextField({
                      allowBlank: true,
                      maxLength: 16,
                      maxText: _Message_MaxText + ' 16',
                      blankText: _NewForText_CanNotEmpty,
                      regex: /^[0-9.]{5,16}$/,
                      regexText: _NewForText_SIMNumberValid
                  }))
                  },
        //{ id: 'BaseMileage', dataIndex: 'BaseMileage', header: _TrackerMain_BaseMileage, editable: true, editor: new Ext.grid.GridEditor(new Ext.form.NumberField({ allowBlank: false })) },  
                  {id: 'TrackerType', dataIndex: 'TrackerType', header: _ImageButtonText_TrackerType, editable: true, editor: trackertypeGridEditor }
                , { id: 'TrackerIconName', dataIndex: 'TrackerIconName', header: _ImageButtonText_TrackerIcon, editable: true, editor: trackerIconGridEditor }
                  ];

        //        var fuelsensorCombo = new Ext.form.ComboBox({
        //            editable: false,
        //            name: "fuelsensorcombo",
        //            allowBlank: false,
        //            mode: "local",
        //            store: new Ext.data.SimpleStore({
        //                    fields: [{name:"id",type:'integer'}, "name"],
        //                    data: [[0,_TrackerMain_Null],[1,_TrackerMain_FuelC],[2,_TrackerMain_FuelR],[3,_TrackerMain_FuelSensorV1]]
        //                    }),
        //            valueField: "id",
        //            displayField: "name",
        //            triggerAction: "all",
        //            hiddenName: "name"
        //        });
        //        
        //        var tanksizeTextEditor = new Ext.form.NumberField({
        //            maxLength:4
        //        });

        //        var fuelsensorEditor = new Ext.grid.GridEditor(fuelsensorCombo);
        //        
        //        var tanksizeEditor = new Ext.grid.GridEditor(tanksizeTextEditor);

        //        function setDisableProperEditorByType(typeid){
        //            if(typeid == 8 || typeid == 13){
        //                fuelsensorCombo.readOnly = false;
        //                
        //            }
        //            else{
        //                fuelsensorCombo.readOnly = true;
        //                fuelsensorCombo.setRawValue(_TrackerMain_CanNotEdit);
        //                fuelsensorEditor.setValue(_TrackerMain_CanNotEdit);
        //            }
        //            if(typeid == 1 || typeid == 2 || typeid == 5 || typeid == 7 || typeid == 14){
        //                tanksizeTextEditor.readOnly = true;
        //                tanksizeTextEditor.setRawValue(_TrackerMain_CanNotEdit);
        //            }
        //            else{
        //                tanksizeTextEditor.readOnly = false;
        //            }
        //        }



        //        if(hasstaticsensorfunction){
        ////            trackerFields.push(new Ext.data.Field({
        ////                id:'TankSize',
        ////                dataIndex:'TankSize',
        ////                header:_TrackerMain_TankSize,
        ////                editable: true,
        ////                editor:tanksizeEditor
        ////            }));
        //            trackerFields.push(new Ext.data.Field({
        //               id:'SensorName1',
        //               dataIndex:'SensorName1',
        //               header:_TrackerMain_FuelSensor,
        //               editable: true,
        //               editor:fuelsensorEditor
        //            }));
        //            
        //            

        //        }


        var propertyGrid = new Ext.ux.grid.PropertyGrid({
            header: false,
            hideHeaders: true,
            autoHeight: true,
            width: 400,
            fields: trackerFields,
            buttons: []
        });

        if (permission >= 0) {
            propertyGrid.addButton(btnConfirm);
            //propertyGrid.addButton(synBtnConfirm);
        }
        if (permission >= 2)
            propertyGrid.addButton(buttonChangeDeviceID);

        propertyGrid.addListener('beforerender', function (propGrid) {
            setMyPropertyGridHeader(propGrid);
        });

        var propertyStore = new Ext.data.JsonStore({
            url: 'HandlerGetDeviceInfo',
            root: 'data',
            fields: ['SN_IMEI_ID', 'TrackerPassWord', 'TrackerName', 'TrackerSimNumber', 'APN', 'GprsUserName', 'GprsPassWord', 'TrackerRegisterTime', 'TrackerActivated', 'TrackerDaysRemain', 'TrackerType', 'TrackerIconName', 'BaseMileage', { name: 'SensorId1', type: 'integer' }, 'SensorName1', { name: 'TankSize', type: 'float' }],
            listeners: {
                load: {
                    fn: function (store, records, options) {
                        var propGrid = propertyGrid;
                        if (propGrid) {
                            //                            var r = records[0];
                            //                            if(r.get('SensorId1') ==1)
                            //                                r.set('SensorName1',_TrackerMain_FuelC);
                            //                            if(r.get('SensorId1') == 2)
                            //                                r.set('SensorName1',_TrackerMain_FuelR);
                            propGrid.setSource(store.getAt(0).data);
                            trackerIconText.setValue(store.getAt(0).data.TrackerIconName);
                            var typename = store.getAt(0).data.TrackerType;

                        }
                    }
                }
            }
        });

        propertyStore.load({ params: { DeviceID: node.id} })

        var form = new Ext.form.FormPanel({
            width: 450,
            height: 450,
            items: [propertyGrid, iconPanel]
        });

        GridMain(node, form, "Tracker");
}

	function getAccountStatusFromText(accountStatus){
		if( accountStatus == _TrackerMainUserDevice_No){
				return 0;
		}
		if( accountStatus == _TrackerMainUserDevice_Yes){
				return 1;
		}
		if( accountStatus == _TrackerMain_Lock){
				return -1;
		}
			
		return -1;
    }
	function getPermissionIDFromTexxt(permission){
		
		if(permission==_NewForText_NormalUser){
			return 1;
		}
		if(permission==_NewForText_Administrator){
			return 2;
		}
		return -1;
	}